
# Makefile for Python Docker Containerization
# Version: 1.0.0

# --------------------------
# 用户可配置变量
# --------------------------
APPLICATION := scorpio
VERSION ?= 0.1.0
IMAGE_NAME ?= scorpio
IMAGE_TAG ?= latest
PACKAGE_MANAGER ?= uv
TARGET_PLATFORM ?= linux/amd64
CONTAINER_NAME ?= scorpio-agent 
PORT ?= 8000
VENV_PATH ?= .venv
PROJECT_DIR ?= $(CURDIR)
PIP_INDEX_URL ?= https://mirrors.aliyun.com/pypi/simple
UV_DEFAULT_INDEX = $(PIP_INDEX_URL)

# --------------------------
# 内部变量
# --------------------------
DOCKERFILE := Dockerfile
DOCKER_IGNORE := .dockerignore
BUILD_SCRIPT := build-image.sh
ENTRYPOINT_SCRIPT := entrypoint.sh
DOCKER_BUILDKIT := 1
BUILD_ARGS := --build-arg PACKAGE_MANAGER=$(PACKAGE_MANAGER)

# --------------------------
# 文件路径
# --------------------------
PYCACHE_PATTERN := __pycache__ *.pyc *.pyo
IGNORE_FILES := .vscode .idea .git .DS_Store .pytest_cache .coverage
BUILD_ARTIFACTS := dist build *.egg-info py/*.egg-info py/scorpio/*.egg-info
TEST_FILES := tests .pytest_cache htmlcov .coverage

# --------------------------
# 主要命令
# --------------------------

.PHONY: all
all: clean build

.PHONY: help
help:
	@echo "Python Docker 容器化构建系统"
	@echo "命令:"
	@echo "  make clean        - 清理Python缓存和构建产物"
	@echo "  make build        - 构建Docker镜像"
	@echo "  make run          - 运行容器"
	@echo "  make shell        - 进入运行中的容器shell"
	@echo "  make stop         - 停止容器"
	@echo "  make logs         - 查看容器日志"
	@echo "  make test         - 运行单元测试"

# --------------------------
# 清理操作
# --------------------------

.PHONY: clean
clean: clean-pyc clean-build clean-test clean-cache

.PHONY: clean-pyc
clean-pyc:
	@echo "清理Python缓存文件..."
	@find $(PROJECT_DIR) -type d -name '__pycache__' -exec rm -rf {} + || true
	@find $(PROJECT_DIR) -type f -name '*.py[co]' -delete || true

.PHONY: clean-build
clean-build:
	@echo "清理构建产物..."
	@rm -rf $(BUILD_ARTIFACTS) || true

.PHONY: clean-test
clean-test:
	@echo "清理测试文件..."
	@rm -rf $(TEST_FILES) || true

.PHONY: clean-cache
clean-cache:
	@echo "清理包管理缓存..."
	@rm -rf .pdm-build .pdm-python .uv_cache || true

# --------------------------
# Docker 构建与运行
# --------------------------

.PHONY: build
build: clean
	@echo "构建pip安装包: ${APPLICATION}-${VERSION}-py3-none-any.whl"
	PIP_INDEX_URL=$(PIP_INDEX_URL) ./scripts/build.sh

.PHONY: build-docker
build-docker: clean
	@echo "构建Docker镜像: $(IMAGE_NAME):$(IMAGE_TAG)"
	./scripts/build_image.sh
	
	
